package com.uzai.console.service.app.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.uzai.common.enums.MerConfigKeyEnum;
import com.uzai.common.enums.MerConfigTypeEnum;
import com.uzai.common.enums.RedisCacheKeyEnum;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.config.UzaiConsoleSysConfig;
import com.uzai.console.dto.app.personalCenter.AppPersonalCenterConfigDto;
import com.uzai.console.entity.MerBaseConfig;
import com.uzai.console.mapper.MerBaseConfigMapper;
import com.uzai.console.service.app.AppPersonalCenterService;
import com.uzai.console.vo.app.personalcenter.AppPersonalCenterConfigVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class AppPersonalCenterServiceImpl implements AppPersonalCenterService {

    @Autowired
    private MerBaseConfigMapper merBaseConfigMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private  UzaiConsoleSysConfig uzaiConsoleSysConfig;
    @Override
    public Object queryAppPersonCenter(Long merId) {

        AppPersonalCenterConfigVo appPersonalCenterConfigVo=new AppPersonalCenterConfigVo();

        //查询app个人中心配置
        MerBaseConfig merBaseConfig = merBaseConfigMapper.selectByMerId(merId, MerConfigKeyEnum.APP_PERSONAL_CENTER_CONFIG.getValue());

        if(merBaseConfig!=null){
            appPersonalCenterConfigVo.setAppPersonalCenterConfig(merBaseConfig.getVal());
        }else {
            //没有配置，读取默认配置
//             merBaseConfig = merBaseConfigMapper.selectByMerId(merId, MerConfigKeyEnum.CMS_PERSONAL_CENTER_CONFIG.getValue());

            Long defaultMerId = uzaiConsoleSysConfig.getDefaultMerId();
            //没有配置，读取默认配置
            merBaseConfig = merBaseConfigMapper.selectByMerId(defaultMerId, MerConfigKeyEnum.APP_PERSONAL_CENTER_CONFIG.getValue());

            if(merBaseConfig==null||merBaseConfig.getVal()==null){
                throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "无CMS个人中心配置");
            }

            JSONObject jsonObject = JSON.parseObject(merBaseConfig.getVal());
            JSONArray components = jsonObject.getJSONArray("components");
            for (int i = 0; i < components.size(); i++) {
                JSONObject jsonObject1 = components.getJSONObject(i);
                if (!jsonObject1.getString("cnName").equals("八宫格")) {
                    continue;
                }
                JSONObject config = jsonObject1.getJSONObject("config");
                JSONArray list = config.getJSONArray("list");
                for (int j = 0; j < list.size(); j++) {
                    JSONObject jsonObject2 = list.getJSONObject(j);
                    if(jsonObject2.get("title")!=null){
                        if(jsonObject2.getString("title").equals("转链")||jsonObject2.getString("title").equals("搜索")){
                            list.remove(j);
                            i--;
                        }
                    }
                }
            }
            appPersonalCenterConfigVo.setAppPersonalCenterConfig(jsonObject.toString());
        }

        return appPersonalCenterConfigVo;
    }

    @Override
    public Object copyCmsPersonCenter(Long merId) {
        
        AppPersonalCenterConfigVo appPersonalCenterConfigVo=new AppPersonalCenterConfigVo();

        MerBaseConfig merBaseConfig = merBaseConfigMapper.selectByMerId(merId, MerConfigKeyEnum.CMS_PERSONAL_CENTER_CONFIG.getValue());

        if(merBaseConfig==null||merBaseConfig.getVal()==null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "无CMS个人中心配置");
        }

        JSONObject jsonObject = JSON.parseObject(merBaseConfig.getVal());
        JSONArray components = jsonObject.getJSONArray("components");
        for (int i = 0; i < components.size(); i++) {
            JSONObject jsonObject1 = components.getJSONObject(i);
            if (!jsonObject1.getString("cnName").equals("八宫格")) {
                continue;
            }
            JSONObject config = jsonObject1.getJSONObject("config");
            JSONArray list = config.getJSONArray("list");
            for (int j = 0; j < list.size(); j++) {
                JSONObject jsonObject2 = list.getJSONObject(j);
                if(jsonObject2.get("title")!=null){
                if(jsonObject2.getString("title").equals("转链")||jsonObject2.getString("title").equals("搜索")){
                    list.remove(j);
                    i--;
                }
                }
            }
        }
        System.out.println(jsonObject);
        appPersonalCenterConfigVo.setAppPersonalCenterConfig(jsonObject.toString());

        return appPersonalCenterConfigVo;
    }


    @Override
    public void updateAppPersonCenter(AppPersonalCenterConfigDto appPersonalCenterConfigDto) {


        if(StringUtils.isBlank(appPersonalCenterConfigDto.getAppPersonalCenterConfig())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入APP个人中心配置内容");
        }

        MerBaseConfig merBaseConfig = merBaseConfigMapper.selectByMerId(appPersonalCenterConfigDto.getMerId(), MerConfigKeyEnum.APP_PERSONAL_CENTER_CONFIG.getValue());

        Integer now = DateUtil.getNowTime();

        if(merBaseConfig!=null){
            merBaseConfig.setVal(appPersonalCenterConfigDto.getAppPersonalCenterConfig());
            merBaseConfig.setUpdateTime(now);
            merBaseConfigMapper.update(merBaseConfig);
        }else {
            merBaseConfig = new MerBaseConfig();
            merBaseConfig.setId(IdWorker.getId());
            merBaseConfig.setMerId(appPersonalCenterConfigDto.getMerId());
            merBaseConfig.setType(MerConfigTypeEnum.PRODUCTS.getValue());
            merBaseConfig.setKey(MerConfigKeyEnum.APP_PERSONAL_CENTER_CONFIG.getValue());
            merBaseConfig.setVal(appPersonalCenterConfigDto.getAppPersonalCenterConfig());
            merBaseConfig.setCreateTime(now);
            merBaseConfig.setUpdateTime(now);
            merBaseConfigMapper.insert(merBaseConfig);
        }
        redisTemplate.opsForValue().set(String.format(RedisCacheKeyEnum.APP_PERSONAL_CENTER_CONFIG.getKey(), appPersonalCenterConfigDto.getMerId()), merBaseConfig.getVal());

    }
}
